package com.guinea.core;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.util.ValidatorUtils;
import org.apache.oro.text.perl.Perl5Util;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.FieldChecks;
import org.apache.struts.validator.Resources;

import com.guinea.core.utils.StringUtils;

public class MyValidator extends FieldChecks {
	public static boolean validateEmail(Object bean, ValidatorAction va,
			Field field, ActionMessages errors, Validator validator,
			HttpServletRequest request) {

		String value = null;
		if (isString(bean)) {
			value = (String) bean;
		} else {
			value = ValidatorUtils.getValueAsString(bean, field.getProperty());
		}
		Perl5Util matcher = new Perl5Util();
		if (!GenericValidator.isBlankOrNull(value)
				&& !matcher.match("/^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/", value.trim())) {
			errors.add(field.getKey(),
					Resources.getActionMessage(validator, request, va, field));
			return false;
		} else {
			return true;
		}
	}

	public static boolean validateAnyphone(Object bean, ValidatorAction va,
			Field field, ActionMessages errors, Validator validator,
			HttpServletRequest request) {

		String value = null;
		if (isString(bean)) {
			value = (String) bean;
		} else {
			value = ValidatorUtils.getValueAsString(bean, field.getProperty());
		}
		Perl5Util matcher = new Perl5Util();
		if (!GenericValidator.isBlankOrNull(value)
				&& !matcher.match("/^\\+?\\d+(-?\\d+)*$/", value)) {
			errors.add(field.getKey(),
					Resources.getActionMessage(validator, request, va, field));
			return false;
		} else {
			return true;
		}
	}

	public static boolean validateZip(Object bean, ValidatorAction va,
			Field field, ActionMessages errors, Validator validator,
			HttpServletRequest request) {

		String value = null;
		if (isString(bean)) {
			value = (String) bean;
		} else {
			value = ValidatorUtils.getValueAsString(bean, field.getProperty());
		}
		Perl5Util matcher = new Perl5Util();
		if (!GenericValidator.isBlankOrNull(value)
				&& !matcher.match("/^[1-9]\\d{5}(?!\\d)$/", value)) {
			errors.add(field.getKey(),
					Resources.getActionMessage(validator, request, va, field));
			return false;
		} else {
			return true;
		}
	}

	public static boolean validatePrecision(Object bean, ValidatorAction va,
			Field field, ActionMessages errors, Validator validator,
			HttpServletRequest request) {
		String value = null;
		if (isString(bean)) {
			value = (String) bean;
		} else {
			value = ValidatorUtils.getValueAsString(bean, field.getProperty());
		}

		if (!GenericValidator.isBlankOrNull(value)) {
			Perl5Util matchAsciiPat = new Perl5Util();
			if (!matchAsciiPat.match("/^(-?\\d+)(\\.\\d+)?$/", value)) {
				errors.add(field.getKey(), Resources.getActionMessage(
						validator, request, va, field));
				return false;
			}
			try {
				int precision = Integer
						.parseInt(field.getVarValue("precision"));
				int scale = Integer.parseInt(field.getVarValue("scale"));
				String scaleValue = StringUtils.substringAfter(value, ".");
				value = value.replaceAll("/[-|\\.]/", "");
				value = value.replaceAll("/^0+", "");

				if (!GenericValidator.maxLength(value, precision)
						|| !GenericValidator.maxLength(scaleValue, scale)) {
					errors.add(field.getKey(), Resources.getActionMessage(
							validator, request, va, field));

					return false;
				}
			} catch (Exception e) {
				errors.add(field.getKey(), Resources.getActionMessage(
						validator, request, va, field));
				return false;
			}
		}

		return true;
	}
}
